﻿using JESAI.Application.Abstractions.Admin;
using JESAI.Application.Abstractions.Admin.Dtos.Menu;
using JESAI.Application.Abstractions.Dtos.Menu;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Biz.Data.Enums;
using JESAI.Core;
using JESAI.Core.Attributes;
using JESAI.Platform.Domain.Admin.Entities;
using JESAI.Platform.Domain.Admin.Repositories;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Admin
{
    [AppService]
    public class MenuService : IMenuService
    {
        private readonly IMenuRepository _menuRepository;
        private readonly IUser _user;
        public MenuService(IMenuRepository menuRepository, IUser user)
        {
            _menuRepository = menuRepository;
            _user = user;
        }
        public List<MenuDto> QueryAllMenus(QueryAllMenusInput input)
        {
            var modelList = _menuRepository._DbQueryable
            .WhereIF(input.MenuType.HasValue, it => it.MenuType == (MenuType)input.MenuType)
            .WhereIF(input.PlatformId > 0, it => it.PlatformId == input.PlatformId)
           .ToList();

            return modelList.MapTo<List<MenuDto>>();
        }

        public List<MenuTreeDto> QueryAllMenuTrees(QueryAllMenusInput input)
        {
            var modelList = QueryAllMenus(input);
            var menuTreeList = modelList.MapTo<List<MenuTreeDto>>();
            return menuTreeList;
        }


        public List<MenuDto> QueryMenusByPlatformIdAndUserId(int platformId, long userId)
        {
            var modelList = _menuRepository._DbQueryable.Context.Queryable<MenuModel, RoleMenuModel, UserRoleModel>((t1, t2, t3) => new object[] { JoinType.Inner, t1.Id == t2.MenuId, JoinType.Inner, t2.RoleId == t3.RoleId })
                                      .WhereIF(platformId > 0, (t1, t2, t3) => t1.PlatformId == platformId)
                                      .Where((t1, t2, t3) => t1.State == 1 && t3.Uid == userId && t1.MenuType == MenuType.Menu)
                                      .OrderBy((t1, t2, t3) => t1.SortId, OrderByType.Asc)
                                      .GroupBy((t1, t2, t3) => t1.Id)
                                      .Select<MenuModel>()
                                      .ToList();

            return modelList.MapTo<List<MenuDto>>();
        }

        public List<MenuDto> QueryUserPageBtns(QueryUserPageBtnInput input)
        {
            var menulList = new List<MenuModel>();
            var menuModel = _menuRepository._DbQueryable.Where(it => it.LinkUrl == input.PagePath && it.State == 1);
            if (menuModel.Count() > 0)
            {
                long uid = _user.Id.ToLong();
                menulList = _menuRepository._DbQueryable.Context.Queryable<MenuModel, RoleMenuModel, UserRoleModel>((t1, t2, t3) => new object[] { JoinType.Inner, t1.Id == t2.MenuId, JoinType.Inner, t2.RoleId == t3.RoleId })
                                         .WhereIF(input.PlatformId > 0, (t1, t2, t3) => t1.PlatformId == input.PlatformId)
                                         .Where((t1, t2, t3) => t1.State == 1 && t3.Uid == uid && t1.MenuType == MenuType.Button && t1.ParentId == menuModel.First().Id)
                                         .OrderBy((t1, t2, t3) => t1.SortId, OrderByType.Asc)
                                         .GroupBy((t1, t2, t3) => t1.Id)
                                         .Select<MenuModel>()
                                         .ToList();


            }

            return menulList.MapTo<List<MenuDto>>();
        }

        public List<MenuDto> QueryUserPageLinkBtns(QueryUserPageBtnInput input)
        {
            var menulList = new List<MenuModel>();
            var menuModel = _menuRepository._DbQueryable.Where(it => it.LinkUrl == input.PagePath && it.State == 1);
            if (menuModel.Count() > 0)
            {
                long uid = _user.Id.ToLong();
                menulList = _menuRepository._DbQueryable.Context.Queryable<MenuModel, RoleMenuModel, UserRoleModel>((t1, t2, t3) => new object[] { JoinType.Inner, t1.Id == t2.MenuId, JoinType.Inner, t2.RoleId == t3.RoleId })
                                         .WhereIF(input.PlatformId > 0, (t1, t2, t3) => t1.PlatformId == input.PlatformId)
                                         .Where((t1, t2, t3) => t1.State == 1 && t3.Uid == uid && t1.MenuType == MenuType.LinkButton && t1.ParentId == menuModel.First().Id)
                                         .OrderBy((t1, t2, t3) => t1.SortId, OrderByType.Asc)
                                         .GroupBy((t1, t2, t3) => t1.Id)
                                         .Select<MenuModel>()
                                         .ToList();


            }

            return menulList.MapTo<List<MenuDto>>();
        }

        public void UpdateOrInsert(SetMenuInput input)
        {
            var model = input.MapTo<MenuModel>();
            if (model.Id > 0)
            {
                _menuRepository.AsUpdateable(model).ExecuteCommand();
            }
            else
            {
                model.PlatformId = 1;
                _menuRepository.AsInsertable(model).ExecuteCommand();
            }
        }
    }
}
